//
//  DiscussDetailsViewController.m
//  BookSale
//
//  Created by dlios on 15-3-10.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "DiscussDetailsViewController.h"

@interface DiscussDetailsViewController ()

@end

@implementation DiscussDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"评价详情";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.disscussTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 554) style:UITableViewStylePlain];
    //隐藏下划线
    //self.disscussTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.disscussTableView.dataSource = self;
    self.disscussTableView.delegate = self;
    [self.view addSubview:self.disscussTableView];
    
    UIImage *backImage = [UIImage imageNamed:@"iconfont-fanhui.png"];
    backImage = [backImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(backBarButtonItemAction:)];
    self.navigationItem.leftBarButtonItem = backButton;
    
    //    self.allUserArray = [NSMutableArray array];
    //    self.isUpLoading = NO;
    
    [self addHeader];
    [self addFooter];
    
}


//下拉刷新
- (void)addHeader
{
    NSLog(@"下拉刷新");
    __block DiscussDetailsViewController *movieVC = self;
    NSLog(@"11111111111");
    [self.disscussTableView addHeaderWithCallback:^{
        NSLog(@"22222222222");
        movieVC.isUpLoading = NO;
        NSLog(@"333333333333");
        [self GetMovieList:0];
        NSLog(@"444444444444");
    }];
    [self.disscussTableView headerBeginRefreshing];
}
//上拉加载
- (void)addFooter
{
    NSLog(@"上拉刷新");
    __block DiscussDetailsViewController *movieVC = self;
    [self.disscussTableView addFooterWithCallback:^{
        NSLog(@"55555555555555");
        movieVC.isUpLoading = YES;
        NSLog(@"666666666666666");
        movieVC.page++;
        [movieVC GetMovieList:movieVC.page];
        
    }];
}



- (void)GetMovieList:(NSInteger)page
{
    NSLog(@"aaaaaaaaaaa");
    
    
    AFNetworkReachabilityManager *netwaorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url = [NSString stringWithFormat:@"http://mapi.dangdang.com/index.php?action=get_product_reviews&user_client=iphone&client_version=5.0.1&product_id=%@&page=%ld&pagesize=10&filtertype=1&postion=get_product_detail&udid=18948B21919333DDC1DDE273A67E5005&time_code=B6A67BE6F6D00E259F6500F41F444B4F&timestamp=1426042804&union_id=537-50&permanent_id=20150304193014100686115327268485015", self.productId, page + 1];
    AFHTTPRequestOperationManager *maneger = [AFHTTPRequestOperationManager manager];
    maneger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", nil];
    [maneger GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netwaorkManager stopMonitoring];
        
        self.allUserDic = responseObject;
        
        
        //        //先取出saleDic字典下的page字典
        //        NSMutableDictionary *pageDic = [responseObject objectForKey:@"page"];
        //        //然后取出pageDic字典下对应的pageindex内容
        //        self.page = [[pageDic objectForKey:@"pageindex"] integerValue];
        //NSLog(@"%@",responseObject);
        if (self.isUpLoading == NO) {
            //说明是下拉刷新，就要清空数组中的数据，保持数组中存放的是最新的数据
            [self.allUserArray removeAllObjects];
        }
        
        NSMutableArray *array = [responseObject objectForKey:@"review_list"];
        //NSLog(@"ddddddddddddddddd%@",array);
        self.allUserArray = [NSMutableArray array];
        for (NSMutableDictionary *dic in array) {
            User *user = [[User alloc] init];
            user.name = [dic objectForKey:@"cust_name"];
            // NSLog(@"3333333333333333%@",user.name);
            user.image_url = [dic objectForKey:@"cust_img"];
            user.body = [dic objectForKey:@"body"];
            //NSLog(@"aaaaaaaaaaaaaaaaaaaaaa%@",user.body);
            [self.allUserArray addObject:user];
            // NSLog(@"2222222222222%@",self.allUserArray);
            [self.disscussTableView reloadData];
            
        }
        [self.disscussTableView footerEndRefreshing];//结束底部刷新效果
        [self.disscussTableView headerEndRefreshing];//结束头部刷新效果
        
        
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"获取数据失败");
    }];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.allUserArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = @"cell";
    DisscussDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[DisscussDetailsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//取消cell点击效果
    User *user = [self.allUserArray objectAtIndex:indexPath.row];
    NSURL *url = [NSURL URLWithString:user.image_url];
    [cell.custimageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"picLib_default@2x.png"]];
    cell.nameLabel.text = user.name;
    cell.bodyLabel.text = user.body;
    //NSLog(@"1111111111111111111%@",user.name);
    CGRect frame = cell.bodyLabel.frame;
    //[self class]返回值为本类
    frame.size.height = [[cell class] heightForText:user.body];
    cell.bodyLabel.frame = frame;
    //    cell.custimageView.frame = CGRectMake(10, 10, 20, 20);
    
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    NSString *key = [self.allUserArray objectAtIndex:indexPath.row];
    //    NSMutableArray *array = [self.allUserDic objectForKey:key];
    User *user = [self.allUserArray objectAtIndex:indexPath.row];
    CGFloat height = [DisscussDetailsTableViewCell heightForText:user.body];
    NSLog(@"****************%f",height);
    
    
    return 80 + height;
    
}




- (void)backBarButtonItemAction:(UIBarButtonItem *)barButton
{
    [self.navigationController popViewControllerAnimated:YES];
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
